import Vue from 'vue'
import Router from 'vue-router'
Vue.use(Router)

const router = new Router({
  // mode: 'history',
  base: process.env.BASE_URL,
  routes: [
    {
      path: '/login',
      name: "login",
      component: () => import("@/views/login/index")
    },
    {
      path: '/',
      name: "home",
      component: () => import("@/views/home/index"),
      children: [
        { name: 'users', path: "users", component: () => import("@/views/user/index") },
        { name: 'rights', path: "rights", component: () => import("@/views/right/index") },
        { name: 'roles', path: "roles", component: () => import("@/views/right/role") }
      ]
    }
  ]
})

router.beforeEach((to, from, next) => {
  const isLogin = sessionStorage.getItem("token") ? true : false;
  if (to.path == "/login") {
    next();
  } else {
    isLogin ? next() : next("/login");
  }
})


export default router